﻿using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;
using EasyXaf.EntityDesigner.BusinessObjects;
using EasyXaf.EntityDesigner.BusinessObjects.Entities.Appearances;
using EasyXaf.EntityDesigner.BusinessObjects.Entities.Associations;
using EasyXaf.EntityDesigner.BusinessObjects.Entities.EntityProperties;
using EasyXaf.EntityDesigner.BusinessObjects.Entities.EntityViews;
using EasyXaf.EntityDesigner.BusinessObjects.Entities.ValidationRules;
using System.ComponentModel;

namespace EasyXaf.EntityDesigner.BusinessObjects.Entities;

/// <summary>
/// 实体
/// </summary>
[XafDisplayName("实体")]
[XafDefaultProperty(nameof(DisplayName))]
public partial class Entity : BaseObject
{
	private string _name;
	private string _namespace;
	private string _caption;
	private string _captionFormat;
	private string _category;
	private string _description;
	private string _imageName;
	private Entity _baseClass;
	private EntityProperty _defaultProperty;
	private string _tableName;
	private string _validationMessage;
	private DefaultBoolean _isCloneable;
	private DefaultBoolean _isCreatable;
	private bool _isAbstract;
	private DefaultBoolean _allowInheritable;
	private DefaultBoolean _isVisibleInNavigations;
	private DefaultBoolean _isVisibleInReports;
	private DefaultBoolean _isVisibleInDashboards;
	private DefaultBoolean _isDeferredDeletion;
	private DefaultBoolean _isOptimisticLocking;
	private DomainModel _domainModel;

	/// <summary>
	/// 名称
	/// </summary>
	[XafDisplayName("名称")]
	[RuleRequiredField]
	public string Name
	{
		get => _name;
		set => SetPropertyValue(nameof(Name), ref _name, value);
	}

	/// <summary>
	/// 显示名称
	/// </summary>
	[XafDisplayName("显示名称")]
	public string DisplayName
	{
		get => GetDisplayName();
	}

	/// <summary>
	/// 命名空间
	/// </summary>
	[XafDisplayName("命名空间")]
	[EditorAlias("LookupStringPropertyEditor")]
	public string Namespace
	{
		get => _namespace;
		set => SetPropertyValue(nameof(Namespace), ref _namespace, value);
	}

	/// <summary>
	/// 标题
	/// </summary>
	[XafDisplayName("标题")]
	public string Caption
	{
		get => _caption;
		set => SetPropertyValue(nameof(Caption), ref _caption, value);
	}

	/// <summary>
	/// 标题格式
	/// </summary>
	[XafDisplayName("标题格式")]
	public string CaptionFormat
	{
		get => _captionFormat;
		set => SetPropertyValue(nameof(CaptionFormat), ref _captionFormat, value);
	}

	/// <summary>
	/// 类别
	/// </summary>
	[XafDisplayName("类别")]
	[EditorAlias("LookupStringPropertyEditor")]
	public string Category
	{
		get => _category;
		set => SetPropertyValue(nameof(Category), ref _category, value);
	}

	/// <summary>
	/// 描述
	/// </summary>
	[XafDisplayName("描述")]
	[Size(SizeAttribute.Unlimited)]
	public string Description
	{
		get => _description;
		set => SetPropertyValue(nameof(Description), ref _description, value);
	}

	/// <summary>
	/// 图像名
	/// </summary>
	[XafDisplayName("图像名")]
	public string ImageName
	{
		get => _imageName;
		set => SetPropertyValue(nameof(ImageName), ref _imageName, value);
	}

	/// <summary>
	/// 基类
	/// </summary>
	[XafDisplayName("基类")]
	[DataSourceProperty(nameof(BaseClassDataSource))]
	public Entity BaseClass
	{
		get => _baseClass;
		set => SetPropertyValue(nameof(BaseClass), ref _baseClass, value);
	}

	/// <summary>
	/// 默认属性
	/// </summary>
	[XafDisplayName("默认属性")]
	[DataSourceProperty(nameof(DefaultPropertyDataSource))]
	public EntityProperty DefaultProperty
	{
		get => _defaultProperty;
		set => SetPropertyValue(nameof(DefaultProperty), ref _defaultProperty, value);
	}

	/// <summary>
	/// 数据表名
	/// </summary>
	[XafDisplayName("数据表名")]
	public string TableName
	{
		get => _tableName;
		set => SetPropertyValue(nameof(TableName), ref _tableName, value);
	}

	/// <summary>
	/// 验证消息
	/// </summary>
	[XafDisplayName("验证消息")]
	public string ValidationMessage
	{
		get => _validationMessage;
		set => SetPropertyValue(nameof(ValidationMessage), ref _validationMessage, value);
	}

	/// <summary>
	/// 可克隆
	/// </summary>
	[XafDisplayName("可克隆")]
	public DefaultBoolean IsCloneable
	{
		get => _isCloneable;
		set => SetPropertyValue(nameof(IsCloneable), ref _isCloneable, value);
	}

	/// <summary>
	/// 可创建
	/// </summary>
	[XafDisplayName("可创建")]
	public DefaultBoolean IsCreatable
	{
		get => _isCreatable;
		set => SetPropertyValue(nameof(IsCreatable), ref _isCreatable, value);
	}

	/// <summary>
	/// 抽象实体
	/// </summary>
	[XafDisplayName("抽象实体")]
	public bool IsAbstract
	{
		get => _isAbstract;
		set => SetPropertyValue(nameof(IsAbstract), ref _isAbstract, value);
	}

	/// <summary>
	/// 允许继承
	/// </summary>
	[XafDisplayName("允许继承")]
	public DefaultBoolean AllowInheritable
	{
		get => _allowInheritable;
		set => SetPropertyValue(nameof(AllowInheritable), ref _allowInheritable, value);
	}

	/// <summary>
	/// 显示在导航中
	/// </summary>
	[XafDisplayName("显示在导航中")]
	public DefaultBoolean IsVisibleInNavigations
	{
		get => _isVisibleInNavigations;
		set => SetPropertyValue(nameof(IsVisibleInNavigations), ref _isVisibleInNavigations, value);
	}

	/// <summary>
	/// 显示在报表中
	/// </summary>
	[XafDisplayName("显示在报表中")]
	public DefaultBoolean IsVisibleInReports
	{
		get => _isVisibleInReports;
		set => SetPropertyValue(nameof(IsVisibleInReports), ref _isVisibleInReports, value);
	}

	/// <summary>
	/// 显示在仪表盘中
	/// </summary>
	[XafDisplayName("显示在仪表盘中")]
	public DefaultBoolean IsVisibleInDashboards
	{
		get => _isVisibleInDashboards;
		set => SetPropertyValue(nameof(IsVisibleInDashboards), ref _isVisibleInDashboards, value);
	}

	/// <summary>
	/// 延迟删除
	/// </summary>
	[XafDisplayName("延迟删除")]
	public DefaultBoolean IsDeferredDeletion
	{
		get => _isDeferredDeletion;
		set => SetPropertyValue(nameof(IsDeferredDeletion), ref _isDeferredDeletion, value);
	}

	/// <summary>
	/// 乐观锁定
	/// </summary>
	[XafDisplayName("乐观锁定")]
	public DefaultBoolean IsOptimisticLocking
	{
		get => _isOptimisticLocking;
		set => SetPropertyValue(nameof(IsOptimisticLocking), ref _isOptimisticLocking, value);
	}

	/// <summary>
	/// 领域模型
	/// </summary>
	[XafDisplayName("领域模型")]
	[Association("Entities-DomainModel")]
	public DomainModel DomainModel
	{
		get => _domainModel;
		set => SetPropertyValue(nameof(DomainModel), ref _domainModel, value);
	}

	/// <summary>
	/// 属性
	/// </summary>
	[XafDisplayName("属性")]
	[Association("Properties-Entity")]
	[DevExpress.Xpo.Aggregated]
	public XPCollection<EntityProperty> Properties
	{
		get => GetCollection<EntityProperty>(nameof(Properties));
	}

	/// <summary>
	/// 关系
	/// </summary>
	[XafDisplayName("关系")]
	[Association("Associations-Entity")]
	[DevExpress.Xpo.Aggregated]
	public XPCollection<Association> Associations
	{
		get => GetCollection<Association>(nameof(Associations));
	}

	/// <summary>
	/// 规则
	/// </summary>
	[XafDisplayName("规则")]
	[Association("ValidationRules-Entity")]
	[DevExpress.Xpo.Aggregated]
	public XPCollection<ValidationRule> ValidationRules
	{
		get => GetCollection<ValidationRule>(nameof(ValidationRules));
	}

	/// <summary>
	/// 外观
	/// </summary>
	[XafDisplayName("外观")]
	[Association("Entity-Appearances")]
	[DevExpress.Xpo.Aggregated]
	public XPCollection<Appearance> Appearances
	{
		get => GetCollection<Appearance>(nameof(Appearances));
	}

	/// <summary>
	/// 视图
	/// </summary>
	[XafDisplayName("视图")]
	[Association("Entity-Views")]
	[DevExpress.Xpo.Aggregated]
	public XPCollection<EntityView> Views
	{
		get => GetCollection<EntityView>(nameof(Views));
	}

	/// <summary>
	/// EntityDesigns
	/// </summary>
	[Browsable(false)]
	[Association("EntityDesigns-Entity")]
	[DevExpress.Xpo.Aggregated]
	public XPCollection<EntityDesign> EntityDesigns
	{
		get => GetCollection<EntityDesign>(nameof(EntityDesigns));
	}

	[Browsable(false)]
	public IList<Entity> BaseClassDataSource
	{
		get => GetBaseClassDataSource();
	}

	[Browsable(false)]
	public IList<EntityProperty> DefaultPropertyDataSource
	{
		get => GetDefaultPropertyDataSource();
	}

	public Entity(Session session)
		: base(session)
	{
	}

	private partial string GetDisplayName();

	private partial IList<Entity> GetBaseClassDataSource();

	private partial IList<EntityProperty> GetDefaultPropertyDataSource();
}
